--
-- Author: shunguo.chen
-- Date: 2018-09-28 09:48:51
--
-- MallVo

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	if data then
		self:init(data)
	end
end

-- message Goods {
-- 	enum NumType {
-- 		NoLimit = 0;		// 没有限制
-- 		DayLimit = 1;		// 每日限制
-- 		OneLifeLimit = 2;	// 一生限制
-- 		WeeklyLimit = 3;  //周限制
-- 	}

-- 	enum CondType {
-- 		None = 0;			// 没要求
-- 		Fame = 1;			// 声望要求
-- 		Level = 2;			// 等级要求
-- 		Vip = 3;			// vip等级要求
-- 		Achievement = 4;	// 成就要求
-- 		GuildAchievement = 5; // 公会成就要求
-- 		AchPoint = 6;		 // 成就点数
-- 		GuildTitle = 7;		 // 公会官职
-- 	}

-- 	message Condition {
-- 		CondType condType = 1;		// 购买要求-类型
-- 		int32 condID = 2;			// 购买要求-ID
-- 		int32 condValue = 3;		// 购买要求-数值/等级
-- 	}

-- 	int32 id = 1;				// 商品ID
-- 	pb_loot.Loot.LootType itemType = 2;// 道具类型
-- 	int32 itemId = 3;			// 道具ID
-- 	repeated Condition conds = 4;		// 条件列表
-- 	pb_loot.Loot.LootType currency = 5;// 货币类型
-- 	int32 price = 6;			// 售价
-- 	NumType numType = 7;		// 数量限制类型
-- 	int32 limitNum = 8;		// 限制数量
-- 	int32 leftNum = 9;			// 剩余数量(可购次数)
-- 	pb_loot.Loot cost = 10;			// 消耗(和currency,price有冗余)

-- 	int64 salesBegTime = 11; // 限时商品开始时间戳
-- 	int64 salesEndTime = 12; // 限时商品结束时间戳
-- 	int64 newEndTime = 13; // new标志结束时间戳
--  int32 srcPrice = 14;  //原价  ,实际 扣费 还用 price 这个字段的价格
--  int32 svrLimitMaxCount = 15 ; //全服限制 最大 数量  >0 全服抢购商品，=0 表示非全服抢购
--  int32 svrLimitNowCount = 16 ; //全服限制, 当前 剩余 数量
-- int64 discountBegTime = 17;  //折扣显示开始时间
-- int64 discountEndTime = 18;  //折扣显示结束时间
-- }
function ClassRef:init(data)
	self.id = data.id

	local conf = GD:queryGoodById(self.id)
	self._conf = conf
	if conf then
		local name  = conf.name
		self._name = name and L(name)
	end

	-- 物品
	self.itemType = data.itemType
	self.itemId = data.itemId
	local itemLoot = {type=data.itemType, id=data.itemId, num=1}
	self.itemData = UD:parseLootToBagItem(itemLoot)

	-- 显示道具
	if conf and conf.fakeShow then
		self.showItemData = UD:parseItemFromString(conf.fakeShow)
	end

	-- 条件
	self.limitLv = 1 -- 限制等级
	local conds = {}
	if data.conds then
		for i,v in ipairs(data.conds) do
		    local cond = {}
		    cond.condType = v.condType --购买要求-类型 --None没要求 Fame声望要求 Level等级要求 Vip vip等级要求 Achievement成就要求
		    cond.condID = v.condID --购买要求-ID
		    cond.condValue = v.condValue -- 购买要求-数值/等级

		   	conds[#conds + 1] = cond

		   	if v.condType == "Level" then
		   		self.limitLv = v.condID
		   	end
		end
	end
	self.conds = conds

	-- 消耗(goods结构更改，商城只支持单个道具购买)
	local costItemData
	if data.cost then
		costItemData = UD:parseLootToBagItem(data.cost[1])
	end
	self.costItemData = costItemData

	-- 数量限制
	self.numType = data.numType
	self.numTypeId =  protobuf.enum_id("shared.Goods.NumType", data.numType)
	self.limitNum = data.limitNum
	self.leftNum = data.leftNum
	self._leftNumCache = data.limitNum

	-- 限时
	self.salesBegTime = data.salesBegTime
	self.salesEndTime = data.salesEndTime

	-- 新／折扣
	self.newEndTime = data.newEndTime
	self.srcPrice = data.srcPrice -- 原价格
	self.price = self.costItemData:getAmount() -- 实际价格
	local discount = self.srcPrice > 0 and self.price/self.srcPrice*10 or 0
	if discount > 0 then
		-- -- 精确到小数点后1位
		-- discount = discount - discount%0.1
		discount = math.floor(discount + 0.5)
	end
	self.discount = discount
	self.discountBegTime = data.discountBegTime
	self.discountEndTime = data.discountEndTime

	-- 全服抢购
	self.svrLimitMaxCount = data.svrLimitMaxCount
	self.svrLimitNowCount = data.svrLimitNowCount
end

function ClassRef:getName()
	return self._name or self.itemData:getName(true)
end

function ClassRef:getDesc()
	if self._conf.desc then
		return L(self._conf.desc)
	elseif self.showItemData then
		return self.showItemData:getDesc()
	else
		return self.itemData:getDesc()
	end
end

function ClassRef:getShowItemData()
	return self.showItemData or self.itemData
end

function ClassRef:getDaibiId()
	return self.costItemData:getId()
end

function ClassRef:getDaibiSmallIcon()
	return self.costItemData:getSmallIcon()
end

function ClassRef:getDaibiName()
	return self.costItemData:getName()
end

-- 折扣可能限时
function ClassRef:isDiscount()
	if self.discountBegTime > 0 and self.discount > 0 then
		local serverTime = app:getServerTime()
		serverTime = math.floor(serverTime)

		local isOpen = false
		local refreshGap
		if serverTime < self.discountBegTime then
			refreshGap = self.discountBegTime - serverTime
		elseif serverTime < self.discountEndTime then
			isOpen = true
			refreshGap = serverTime - self.discountEndTime
		end
		return isOpen, refreshGap
	end
	return false
end

-- 等级是否满足
function ClassRef:isLvEnable(lv)
	lv = lv or UD:getLevel()
	return self.limitLv <= lv
end

-- 一生限购的买完了
function ClassRef:isLifeLimitOver()
	return self.numType == "OneLifeLimit" and self.leftNum == 0
end

function ClassRef:islvShow()
    --限制等级显示
    local goodConfig = GD:queryGoodById(self.id)
    if goodConfig and goodConfig.lvShow then
        return UD:getLevel() >= tonumber(goodConfig.lvShow)
    else
        return true
    end
end

-- 商品是否可见 可见条件 =》1.满足等级 2.一生限购还有次数 3.满足限时
function ClassRef:isVisible()
	if self:islvShow() and not self:isLifeLimitOver() then
		local isOpen = self:checkOpen()
		return isOpen
	end
	return false
end

function ClassRef:isNew()
	local serverTime = app:getServerTime()
	local isNew = self.newEndTime > serverTime
	if isNew then
		return isNew, self.newEndTime - serverTime
	else
		return isNew
	end
end

-- 打开的
function ClassRef:checkOpen()
	if self.salesBegTime > 0 then
		-- 限时
		local serverTime = app:getServerTime()
		serverTime = math.floor(serverTime)

		local isOpen = false
		local refreshGap
		if serverTime < self.salesBegTime then
			refreshGap = self.salesBegTime - serverTime
		elseif serverTime < self.salesEndTime then
			isOpen = true
			refreshGap = self.salesEndTime - serverTime
		end
		return isOpen, refreshGap
	else
		return true
	end
end

function ClassRef:isTimeLimit()
	return self.salesBegTime > 0
end

function ClassRef:isCanRushBuy()
	return self:checkOpen() and 
		self:isRushBuy() and 
		self:getBuyNumMax() > 0
end

function ClassRef:isRushBuy()
	return self.svrLimitMaxCount > 0
end

-- 需要全服刷新
function ClassRef:isNeedRushRefresh()
	return self:checkOpen() and 
		self:isRushBuy() and 
		self.svrLimitNowCount > 0
end

-- 当前可以购买最大数量
function ClassRef:getBuyNumMax()
	if self.numType == "NoLimit" then
		if self:isRushBuy() then
			return self.svrLimitNowCount
		else
			return 999
		end
	else
		if self:isRushBuy() then
			return math.min(self.leftNum, self.svrLimitNowCount)
		else
			return self.leftNum
		end
	end
end

function ClassRef:isDayChange()
	return self.numType == "DayLimit"
end

function ClassRef:setLeftNum(num)
	self.leftNum = num
end

function ClassRef:setRushNum(num)
	self.svrLimitNowCount = num
end

-- 凌晨5点时刷新商品列表，重置每日限购的商品次数调用此接口
function ClassRef:resetLeftNum()
	self.leftNum = self._leftNumCache
end

return ClassRef